<template>
	
	<el-table :data="tableData" style="width: 100%;margin-top: 20px;" >
		<el-table-column fixed prop="borrowTime" label="借书时间" width="150" />
		<el-table-column prop="bookName" label="书名" width="120" />
		<el-table-column prop="bookAuthor" label="作者" width="120" />
		<el-table-column prop="bookRemark" label="书籍编号" width="160" />
		<el-table-column prop="bookPrice" label="价格" width="120" />
		<el-table-column prop="bookType" label="类别" width="120" />
		<el-table-column prop="time" label="到期时间剩余" width="120" >
			<template #default="scope">
			        <el-tag
			          :type="parseInt(scope.row.price) < 10 ? 'danger' : 'success'"
			          disable-transitions
			          >{{ (new Date(scope.row.returnTime).getTime()-new Date(scope.row.borrowTime).getTime())/1000/24/60/60 }}天</el-tag>
			</template>
		</el-table-column>
		<el-table-column fixed="right" label="选项" width="120">
			<template #default="scope">
				<el-button link type="primary" size="small" @click.prevent="deleteRow(scope.row)">归还</el-button>
			 </template>
		</el-table-column>
	</el-table>
	<!-- 分页 -->
	<!-- <el-pagination style="margin-top: 20px;" background layout="prev, pager, next" :total="1000" /> -->
</template>

<script lang="ts" setup>
	
import { onMounted, reactive, ref } from 'vue'
import mitt from '../utils/mitt';
import api from '../api';
const emit = defineEmits(['get-msg'])
	const books=reactive({
		bookAuthor:'',
		bookName:'',
		bookNum:'',
		bookPrice:'',
		bookRemark:'',
		id:'',
		bookId:'',
		
	})
	const deleteRow = (row) => {
		for (const key in row) {
			books[key] = row[key]
		}
	  tableData.value.splice(0, 1)
	  api.returnApi(books).then(res=>{
		  if(res.data.status=='success'){
			  emit('get-msg',res.data.msg)
			  mybow()
		  }
		  
	  })
	  console.log(row)
	}
	
	onMounted(()=>{
		//兄弟组件互传
			mitt.on('myMsg',(data)=>{
				if(data=='flash'){
					mybow()
				}
			})
		mybow()
	})
	const myid=reactive({
		id:''
	})
	const mybow=()=>{
		myid.id=localStorage.getItem('id')
		api.myborrowApi(myid).then(res=>{
			tableData.value=res.data.data
			console.log(res.data.data)
		})
		if(localStorage.getItem('name')==null){
			tableData.value=tableDataback.value
		}
	}
	const tableData = ref([
	  {
		id:'',
	    borrowTime: '登陆后获取数据',
	    bookName: '',
	    bookAuthor: '',
	    bookRemark: '',
	    bookPrice: '',
		bookType: '',
		returnTime:"登陆后获取数据"
	  }
	])
	const tableDataback = ref([
	  {
		id:'',
	    borrowTime: '登陆后获取数据',
	    bookName: '',
	    bookAuthor: '',
	    bookRemark: '',
	    bookPrice: '',
		bookType: '',
		returnTime:"登陆后获取数据"
	  }
	])
	defineExpose({
		mybow
	})
</script>

<style>
</style>